package com.ljkj.blockpay.ui.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseFragment;
import com.ljkj.blockpay.databinding.FragmentMainLoginBinding;
import com.ljkj.blockpay.presenter.PhoneLoginPresenter;
import com.ljkj.blockpay.presenter.inter.LoginInter;
import com.ljkj.blockpay.util.ToggleToActivity;
import com.ljkj.blockpay.widget.ClickListener;
import com.ljkj.blockpay.widget.CommonDialog;
import com.luck.picture.lib.tools.ToastUtils;

public class EmailLoginFragment extends BaseFragment<FragmentMainLoginBinding, PhoneLoginPresenter> implements LoginInter.LoginInterView, ClickListener {

    private CommonDialog useragreementDialog;
    private CommonDialog privacypolicyDialog;
    private String phone;
    private String password;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int initContentView() {
        return R.layout.fragment_main_login;
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void initData() {
        super.initData();
        binding.setOnClick(this::onClick);
//maoyf9@163.com   W2ew2ewW2ewe
        initListener();
    }

    private void initListener() {
        //限制邮箱输入
        binding.etPhone.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        binding.etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                phone = editable.toString().trim();
                if (phone.isEmpty()){
                    ToastUtils.s(getActivity(),"邮箱为空");
                } else {
                    if (!TextUtils.isEmpty(password)&& password.length()>=6){
                        binding.btnLogin.setSelected(true);
                    }
                }
            }
        });

        binding.etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                password = editable.toString().trim();
                if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password) && password.length() >= 6){
                    binding.btnLogin.setSelected(true);
                }

            }
        });


    }

    @Override
    protected PhoneLoginPresenter createPresenter() {
        return new PhoneLoginPresenter(getActivity(), this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tbn_eye:
                if (binding.tbnEye.isChecked()==false){
                    binding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    binding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.rbn_isagree:
                if (binding.rbnIsagree.isChecked()){

                }else {
                }

                break;
            case R.id.tv_useragreement:
                useragreementDialog = CommonDialog.Builder.with(getActivity()).setContentView(R.layout.aboutuser_layout).setOnClickListener(R.id.btn_agree, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        useragreementDialog.dismiss();
                    }
                }).setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        useragreementDialog.dismiss();
                    }
                }).forGravity(Gravity.BOTTOM)
                        .fullWidth()
                        .formBottom(true)
                        .setCancelable(false)
                        .setOnKeyListener((dialog, keyCode, event) -> false)
                        .create();
                useragreementDialog.show();
                break;
            case R.id.tv_privacypolicy:
                privacypolicyDialog = CommonDialog.Builder.with(getActivity()).setContentView(R.layout.aboutuser_layout).setOnClickListener(R.id.btn_agree, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        privacypolicyDialog.dismiss();
                    }
                }).setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        privacypolicyDialog.dismiss();
                    }
                }).forGravity(Gravity.BOTTOM)
                        .fullWidth()
                        .formBottom(true)
                        .setCancelable(false)
                        .setOnKeyListener((dialog, keyCode, event) -> false)
                        .create();
                privacypolicyDialog.show();
                break;
            case R.id.btn_login:
                 if (binding.etPhone.getText().toString().isEmpty()){
                    ToastUtils.s(getActivity(),"请输入邮箱");
                } else if (binding.etPassword.getText().toString().isEmpty()){
                    ToastUtils.s(getActivity(),"请输入密码");
                } else if (binding.rbnIsagree.isChecked()== false){
                    ToastUtils.s(getActivity(),"请同意《C-Pay用户协议》和《隐私政策》");
                } else{
                    String et = binding.etPhone.getText().toString();
                    String pwd = binding.etPassword.getText().toString();
                        binding.btnLogin.setSelected(true);
                        getPresenter().userLogin(getActivity(),"","",et,"",pwd);
                }
                break;
            case R.id.tv_forgetpwd:

                break;
            case R.id.tv_momentregiest:
                ToggleToActivity.toRegiestActivity(getActivity());
                break;
        }
    }

    @Override
    public void loginSuccess() {
        ToggleToActivity.toMainActivity(getActivity());
    }
}